package org.execute.tool.列表对比;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class ListComparison {
    public static void main(String[] args) {
        List<DataObject> list1 = new ArrayList<>();
        list1.add(new DataObject("1", "a"));
        list1.add(new DataObject("2", "b"));
        list1.add(new DataObject("3", "c"));
        list1.add(new DataObject("4", "d"));
        list1.add(new DataObject("5", "e"));
        list1.add(new DataObject("18", "e"));
        list1.add(new DataObject("26", "e"));
        list1.add(new DataObject("38", "e"));
        list1.add(new DataObject("46", "e"));
        list1.add(new DataObject("58", "e"));
        list1.add(new DataObject("66", "e"));
        List<DataObject> list2 = new ArrayList<>();
        list2.add(new DataObject("1", "a"));
        list2.add(new DataObject("2", "a"));
        list2.add(new DataObject("3", "b"));
        list2.add(new DataObject("4", "a"));
        list2.add(new DataObject("5", "c"));
        list2.add(new DataObject("6", "d"));
        list2.add(new DataObject("7", "e"));
        list2.add(new DataObject("8", "e"));
        list2.add(new DataObject("9", "e"));
        list2.add(new DataObject("10", "e"));
        list2.add(new DataObject("11", "e"));

        List<DataObject> dataObjects = listComparison(list1, list2);

        // 使用 for-each 循环遍历 map
       for (DataObject dataObject : dataObjects) {
           System.out.println(dataObject.getKey() + " : " + dataObject.getValue());
       }
    }
   public static List<DataObject> listComparison(List<DataObject> list1, List<DataObject> list2){
        HashSet<DataObject> dataObjectsSet_1 = new HashSet<>(list1);
        HashSet<DataObject> dataObjectsSet_2 = new HashSet<>(list2);
        ArrayList<DataObject> dataObjectsList_1 = null;//长
        ArrayList<DataObject> dataObjectsList_2 = null;//短
        if (dataObjectsSet_1.size() > dataObjectsSet_2.size()) {
            dataObjectsList_1 = new ArrayList<>(dataObjectsSet_1);
            dataObjectsList_2 = new ArrayList<>(dataObjectsSet_2);
        } else {
            dataObjectsList_1 = new ArrayList<>(dataObjectsSet_2);
            dataObjectsList_2 = new ArrayList<>(dataObjectsSet_1);
        }

        List<DataObject> objectObjectList = new ArrayList<>();
        for (DataObject dataObject : dataObjectsList_1) {
            int n=0;
            for (DataObject dataObject1 : dataObjectsList_2) {
                if (dataObject.equals(dataObject1)) {
                    objectObjectList.add(new DataObject(dataObject,dataObject1));
                    dataObjectsList_2.remove(dataObject1); // 从 dataObjectsList_2 中移除已经找到相等的元素
                    n=1;
                    break;
                }
            }
            if (n==0){
                objectObjectList.add(new DataObject(dataObject,null));
            }
        }
        if (dataObjectsList_2.size()>0) {
            for (DataObject dataObject : dataObjectsList_2) {
                objectObjectList.add(new DataObject(null,dataObject));
            }
        }
        return objectObjectList;
    }
}
